package com.yc.store.servlet;

import com.google.gson.Gson;
import com.yc.store.bean.Goods;
import com.yc.store.bean.Result;
import com.yc.store.biz.BizExeption;
import com.yc.store.biz.GoodsBiz;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/4/25
 * Email haijunzhou@hnit.edu.cn
 */
@WebServlet("/delGood")
public class DelGoodServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("id");
        int id = Integer.valueOf(sid);

        WebApplicationContext ioc=
                WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
        GoodsBiz goodsBiz = ioc.getBean(GoodsBiz.class);
        Result result;
        try {
            goodsBiz.remove(id);
            result = new Result(1,"添加删除成功！",null);
        } catch (Exception e) {
            result = new Result(0,"业务繁忙,请稍后再试!",null);
        }

        resp.setContentType("application/json;charset=utf-8");
        Gson gson = new Gson();
        String json = gson.toJson(result);
        resp.getWriter().append(json);
    }
}
